﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Millionaire;

namespace WindowsFormsApplication1.BLL
{
    public class NguoiChoiBLL : BaseBLL, API.INguoiChoiBLL
    {

        #region INguoiChoiBLL Members

        public bool AddNew(NguoiChoi nc)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    ctxt.NguoiChois.InsertOnSubmit(nc);
                    ctxt.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        public bool Update(NguoiChoi nc)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    NguoiChoi edit = ctxt.NguoiChois.SingleOrDefault(x => x.MaNguoiChoi == nc.MaNguoiChoi);
                    if (edit != null)
                    {
                        edit.HoTen = nc.HoTen;
                        ctxt.SubmitChanges();
                        return true;
                    }
                    else
                        return false;

                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        public bool Delete(int idNguoiChoi)
        {
            throw new NotImplementedException();
        }

        public NguoiChoi GetByID(int idNguoiChoi)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.NguoiChois.SingleOrDefault(x => x.MaNguoiChoi == idNguoiChoi);
            }
        }

        public List<NguoiChoi> GetAll()
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.NguoiChois.ToList();
            }
        }

        public NguoiChoi GetByLuotChoi(int idLuotChoi)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
